package com.diver.flinkdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diver.flinkdemo.entity.xxt.StudentCardGuardSetting;
import com.diver.flinkdemo.mapper.mysqlMapper.StudentCardGuardSettingMapper;
import com.diver.flinkdemo.service.IStudentGuardSettingService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;


/**
 * @Author liuC
 * @Date 2022/12/12 0012 10:39
 * @Describe
 */
//@Service
 @Slf4j
public class StudentGuardSettingServiceImpl implements IStudentGuardSettingService {

    private StudentCardGuardSettingMapper mapper;
    private SqlSession session;
//    @Override
    public StudentCardGuardSetting getGuardSettingOfOpen(String cardNum) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession(true);
            mapper = session.getMapper(StudentCardGuardSettingMapper.class);
            LambdaQueryWrapper<StudentCardGuardSetting> guardSettingWrapper = new LambdaQueryWrapper<>();
            guardSettingWrapper.eq(StudentCardGuardSetting::getCardNum,cardNum);
            guardSettingWrapper.eq(StudentCardGuardSetting::getStatus,1);
            guardSettingWrapper.eq(StudentCardGuardSetting::getDelFlag,0);
            guardSettingWrapper.last("limit 1");
            return mapper.selectOne(guardSettingWrapper);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
